﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using FortuneArterial.Site.Laboratory.Models;
using System.ComponentModel;

namespace FortuneArterial.Site.Laboratory.Controllers
{
    public class HomeController 
        : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            //获取本站点所有Controller和Action
            List<NavItem> list = new List<NavItem>();

            var controllers = (from t in Assembly.GetExecutingAssembly().GetTypes()
                where t.IsSubclassOf(typeof (Controller))
                    && t != this.GetType() && t != typeof(BaseController)
                select t).ToArray();
            foreach (var controller in controllers)
            {
                NavItem ns = null;
                //先找命名空间
                var nsName = controller.Namespace.Substring("FortuneArterial.Site.Laboratory.Controllers".Length);
                if (nsName != "")
                {
                    nsName = nsName.Substring(1);
                    ns = list.FirstOrDefault(n => n.Name == nsName);
                    if (ns == null) {
                        ns = new NavItem() { 
                            Name = nsName,
                            Text = nsName
                        };
                        list.Add(ns);
                    }
                }

                var DisplayNameAttr = controller.GetCustomAttribute(typeof(DisplayNameAttribute)) as DisplayNameAttribute;
                var clsName = controller.Name.Remove(controller.Name.Length - "Controller".Length);

                var model = new NavItem()
                {
                    Name = clsName,
                    Text = DisplayNameAttr != null ? DisplayNameAttr.DisplayName : clsName
                };
                if (ns == null)
                {
                    list.Add(model);
                }
                else {
                    ns.Children.Add(model);
                }

                var actions = (from m in controller.GetMethods()
                    where m.IsPublic && m.ReturnType.IsAssignableFrom(typeof (ActionResult))
                        //没有请求限制或限制为Get请求
                        && (!m.GetCustomAttributes(typeof(ActionMethodSelectorAttribute)).Any()
                            || m.GetCustomAttributes(typeof(ActionMethodSelectorAttribute)).Any(a => a is HttpGetAttribute))
                        //不是属性或事件
                        && !m.Name.StartsWith("get_") && !m.Name.StartsWith("set_") && !m.Name.StartsWith("add_") && !m.Name.StartsWith("remove_")
                    select m).ToArray();
                foreach (var action in actions)
                {
                    var actionNameAttr = action.GetCustomAttribute(typeof(ActionNameAttribute)) as ActionNameAttribute;
                    var aName = actionNameAttr != null ? actionNameAttr.Name : action.Name;
                    var aDisplayNameAttr = action.GetCustomAttribute(typeof(DisplayNameAttribute)) as DisplayNameAttribute;
                    model.Children.Add(new NavItem()
                    {
                        Name = aName,
                        Text = aDisplayNameAttr != null ? aDisplayNameAttr.DisplayName : aName
                    });
                }
            }

            return View(list);
        }

    }
}
